$(function () {
    getUserInfo()

    //退出功能
    $('#logout').click(function () {
        //eg1
        layer.confirm('确定退出么?', { icon: 3, title: '提示' }, function (index) {
            //do something
            location.href = '/login.html'
            localStorage.removeItem('token')

            layer.close(index);
        });
    })
});

//获取用户信息
function getUserInfo() {
    axios({
        // 请求方式是 GET　的时候　可以省略
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // }
    }).then(res => {
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        // 渲染头像
        renderAvatar(res.data.data)
    })
};

// 渲染用户头像
function renderAvatar(user) {
    let name = user.nickname || user.username
    let first = name[0].toUpperCase()
    if (user.user_pic !== null) {
        $('.user').hide()
        $('.layui-nav-img').show().attr('src', user.user_pic)
    } else {
        $('.user').show().html(first)
        $('.wlecome').html('欢迎&nbsp;' + name)
        $('.layui-nav-img').hide()
    }
}
